package com.odin.mysql.config;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


/**
 * @Author ICE
 * @Date 2018-10-03
 **/

@Configuration
@EnableTransactionManagement
public class MyBatisConfig {

    @Value("${mybatisPlus.globalConfig.dbConfig.idType}")
    IdType idType;
    @Value("$[mybatisPlus.globalConfig.dbConfig.fieldStrategy}")
    FieldStrategy fieldStrategy;

    /**
     * 全局配置
     */
    @Bean(name = "myGlobalConfig")
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        // ID自增
        dbConfig.setIdType(idType);
        dbConfig.setFieldStrategy(fieldStrategy);
        globalConfig.setDbConfig(dbConfig);

        return globalConfig;
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Primary
    @Bean(name = "mySqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("myDruidDataSource") DataSource dataSource,
                                               @Qualifier("myGlobalConfig") GlobalConfig globalConfig) throws Exception {
        // mapper映射的xml文件存放地址
        String mapperLocations = "classpath:mapper/**/*.xml";
        // mybatis的xml配置文件存放地址
        String configLocation = "classpath:mybatis-config.xml";
        // pojo/model文件存放地址
        String typeAliasesPackage = "com.odin.**.model.**";
        // 枚举文件存放地址
        String typeEnumsPackage = "com.odin.**.model.enums.**";

        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setGlobalConfig(globalConfig);

        // 扫描pojo模型包
        bean.setTypeAliasesPackage(typeAliasesPackage);
        // 扫描enum枚举包
        bean.setTypeEnumsPackage(typeEnumsPackage);
        // 扫描Mapping.xml文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources(mapperLocations));
        // 扫描Mybatis-config.xml文件
        bean.setConfigLocation(resolver.getResource(configLocation));
        return bean.getObject();

    }

    /**
     * 配置*Mapper扫描的包
     */
    @Bean(name = "myMapperScannerConfigurer")
    public MapperScannerConfigurer mapperScannerConfigurer() {
        // mapper扫描包对应地址
        String basePackage = "com.odin.**.mapper.**";
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(basePackage);
        return configurer;
    }

    /**
     * 事务处理器
     */
    @Bean(name = "myDataSourceTransactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("myDruidDataSource") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}
